package com.xiao.myself.slide.utils;

import java.math.BigDecimal;

import android.content.Context;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.xiao.slipUtils.D;

public class QQSlideUtils<T extends ViewGroup> implements OnTouchListener {


	Context  mcontext;
	//�ж��Ƿ��������
	public static boolean hideFinish=false;
	RelativeLayout mllLeftlayout;
	T mllRightLayout;
	RelativeLayout mRLlayout;

	View mView;
	//�����м䲼�ֲ���
	LayoutParams mCenterPrams;
	//�ұ߲��ֲ���
	LayoutParams mRightParams;
	//������߲��ֲ���
	LayoutParams mLeftParams;
	//��߲��ֲ���
	//adapterview �����¼�
	OnItemClickListener mItemClickListener;
	//���ڼ�ס��Ļ��80%λ��
	int screen8;
	//��Ļ��20%
	int screen2;
	//��ס���ұ߿���ռ��Ļ�ĳߴ�
	int mleftScreenLength; 
	int mDownX;   //���µĺ����
	int mUpX;  //̧��ĺ����
	int mDownY;

	//basedapter����
	View mViewAdapter;
	public QQSlideUtils() {
		super();
	}




	int mScreenWith;   //������Ļ���
	public QQSlideUtils(Context mcontext, RelativeLayout mLLlayout, T mllrightLayout,
			RelativeLayout mRLlayout, View mView) {
		super();
		this.mcontext = mcontext;
		this.mllLeftlayout = mLLlayout;
		this.mllRightLayout = mllrightLayout;
		this.mRLlayout = mRLlayout;
		this.mView = mView;
		initMenuShow();
		setTouchListener(mRLlayout);

	}




	/**
	 * @param mView
	 */
	public void setTouchListener(View mView) {
		//				this.mView=mView;
		mView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return QQSlideUtils.this.onTouch(v, event)||D.LeftMenu.isShow||D.RightMenu.isRightShow;
			}
		});
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mDownX=(int) event.getRawX();
			mDownY=(int) event.getRawY();
			break;

		case MotionEvent.ACTION_UP:
			mUpX=(int) event.getRawX();
			//�õ�̧��ʱ��Y���
			//�������̧��ľ���С��30����listview�Լ�����
			int length=Math.abs(mUpX-mDownX);
			//��̧��ʱ ����Y�ƶ��ľ���
			int upY=(int) event.getRawY();
			//����Y �ƶ��ľ���
			int ylength=upY-mDownY;
			int y=Math.abs(ylength);
			if(y<20&&length<20){//��YС��20ʱ  ����Ϊ�ǵ���¼�
				
				
				return false;
			}
			//���Y�ƶ��ľ������X�ƶ��ľ����򷵻�false
			if(y>length){
				return false;
			}
			if(length<=(screen8)){
				//�����˵��Ѿ���ʾ��ʱ��  ������ ��ʱ�ֲ˵�δ����
				//				hideLeftMenuListener();
				hideLeftAndCenterMenu(mCenterPrams.leftMargin);
			}else if(length>(screen8/2)&&(mUpX>mDownX)){
				//����ܻ����ľ��볬��������Ļ��һ�룬��̧��ʱ�����Զ�����˵�
				//				showLeftMenuListener();
			}




			break;

		case MotionEvent.ACTION_MOVE:
			//�õ��ƶ�ʱY�����
			int moveY=(int) event.getRawY();
			//����Y���ƶ�ʱ�Ͱ���ʱ�ľ���
			int Ylength=Math.abs(moveY-mDownY);
			int X=(int) event.getRawX();
			int abs=X-mDownX;
			int Xlength=Math.abs(abs);
			if(Math.abs(abs)<Ylength){
				return false;
			}
     //���һ���ʱֻ��Ҫ�ƶ�һ��
//			int i=0;
			//�жϻ����ķ���  
			if(X>=mDownX){ 

				//���ʱ���һ���
				mllLeftlayout.setVisibility(1);
				slideToRightOfCenterView(Xlength);
			}else{
				//��ʱ���󻬶�
				//				hideLeftAndCenterMenu(mCenterPrams.leftMargin);
				//				Log.i("main", "mCenterPrams.leftMargin="+mCenterPrams.leftMargin);
				//				Log.i("main", "Xlength="+Xlength);
				//				
				//				Log.i("main", "mDownX="+mDownX);
				//				Log.i("main", "X="+X);
				//				slideToRightOfCenterView(X);
				
				//ֻ�ƶ�һ��   ������Ҫ�ж�
//				if(i==0){
//					
//					hideLeftAndCenterMenu(0);
//				}
//				i++;
			}
			//			
			break;

		}

		return true;
	}



	/**
	 * //��ʱ���󻬶�
	 * @param i
	 */
	private void slideToLeftOfCenterView(int i) {
		// TODO Auto-generated method stub

	}




	/**
	 * ����˵����м�˵����ڵ�ʱ�� ��������
	 */
	public void  hideLeftAndCenterMenu(int x){
		new HideLeftAndCenterMenu(false, true).execute(x);
	};

	/**
	 * ��������  ����˵����м�˵�����ʱ
	 * @author lenovo
	 *
	 */
	class HideLeftAndCenterMenu extends AsyncTask<Integer, Void, Void>{
		boolean showMenu;  //��ס��˵�����״̬  
		boolean   center;//�����߲˵�  Ϊtrueʱ���ʾ��ʱ���������ʾ Ϊfalse���ʱ���ڽ����Ҳ����
		//20%��Ļ����ռÿ���ƶ�10���ı���
		float scaley;
		//������˵�Y�Ὺʼ����Ϊ1
		float mleftScale=1;
		//������ס���ݹ�����X �ĳ���
		int xlength;
		//��סÿ�����ӵı���
		float mscale=(float) 0.8;
		//��ִ�е���ʱ  ����onprogress������ű����Ƿ�ﵽ���  ��ȫ���Y��
		boolean lastone;
		public HideLeftAndCenterMenu(boolean showMenu, boolean center) {
			super();
			this.showMenu = showMenu;
			this.center = center;

		}


		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			mllLeftlayout.setFocusable(false);
			mllLeftlayout.setActivated(false);
			mllLeftlayout.setClickable(false);
		}
		@Override
		protected Void doInBackground(Integer... params) {
			xlength=params[0];
			// ����ÿ���ƶ�10px��ѭ������
			int count=0;
			if(xlength!=0){
				mCenterPrams.leftMargin=xlength;
				//�õ�����10֮��Ҫ�ƶ��Ĵ���
				count=getCount(xlength);
				//				count=xlength/10;
			}else{
				//��ʱ��ʾ  ��ָһ����̧��
				//�õ�ÿ���ƶ�10����Ҫ�ƶ��Ĵ���
				count = (int) ((mScreenWith*0.8) / 10);
			}
			//����������ÿ��Ҫ�ƶ��ı���
			scaley=div(0.2,count,9); 
			//					(0.2/ (float)count);

			// ѭ�������˵�ÿ�������ƶ�10px
			for (int i = 1; i <= count; i++) {
				if(i==(count-1)){
					//��ִ�е���ʱ  ����onprogress������ű����Ƿ�ﵽ���  ��ȫ���Y��
					lastone=true;
				}
				publishProgress();
				SystemClock.sleep(5);// ��ͣ10����
			}
			//		if(center){
			//�������߲˵�  ��ֵ����߲˵�
			D.LeftMenu.isShow=showMenu;

			return null;
		}
		/**
		 * //�õ�����10֮��Ҫ�ƶ��Ĵ���
		 * @return 
		 */
		private int getCount(int c) {
			// TODO Auto-generated method stub
			int x=c%10;
			if(x==0){
				return c/10;
			}else{
				return c/10+1;
			}

		}


		@Override
		protected void onProgressUpdate(Void... values) {
			//��ȡparam ��ֵ
			//���м䲼�ֲ�����߸�ֵΪ��Ļ��80%
			//			LayoutParams paramCenter = getCenterParam();
			LayoutParams paramLeft=getLeftParam();

			mCenterPrams.leftMargin -= 10;// �������˵�
			//�����ƶ�����ܳ�����Ļ��-80%
			mCenterPrams.leftMargin=(int) Math.max(0, mCenterPrams.leftMargin);
			// ֪ͨϵͳ����mLayoutLeftMenu��onDraw()�ػ�
			//			mRLlayout.invalidate();
			if(lastone){
				//����Y���ϵ����ű��� Ϊ���
				mView.setScaleY(1);
			}else{
				//����Y���ϵ����ű���  ������
				mView.setScaleY(mscale);
			}

			//
			mscale=(float) mscale+scaley;
			/*
			 *������˵����˲��� 
			 */
			//������˵���߻����ľ���
			paramLeft.leftMargin-=10/4;
			//��������ߵ�������  Ϊ��Ļ��20%
			paramLeft.leftMargin=(int) Math.max(-screen2, paramLeft.leftMargin);

			//�������Y�����˵ı���
			mllLeftlayout.setScaleY(mleftScale);
			mleftScale-=scaley;
			//�����м�˵����ֲ���
			mView.setLayoutParams(mCenterPrams);
			//�������ɲ˵�������˵�����
			mllLeftlayout.setLayoutParams(paramLeft);

		}



	}

	/**
	 * �õ���߲����
	 * @return
	 */
	private LayoutParams getLeftParam() {
		// ��ȡ���˵��Ĳ��ֲ���
		LayoutParams param = null;
		//		if(center){
		// ��ȡ���˵��Ĳ��ֲ���
		param = (LayoutParams) mllLeftlayout.getLayoutParams();
		return param;
	}

	/**
	 * ������
	 * @param d
	 * @param d2
	 * @param len
	 * @return
	 */
	public static float div(double d,
			double d2,int len) {// ���г�����
		BigDecimal b1 = new BigDecimal(d);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.divide(b2,len,BigDecimal.
				ROUND_HALF_UP).floatValue();
	}

	private void slideToRightOfCenterView(int Xlength) {



		//�����Y����������ĻY��20% ���������������һ���ʱ�����ŵ�Y 20%  ��Ӧ���һ���80%  ����  (0.2/screen8)*Xlength
		float scale=div(0.2, screen8, 8)*Xlength;
		//				(float) ((0.2/screen8)*Xlength);
		//��ʱ���һ���
		//				mCenterPrams.rightMargin=mScreenWith+Xlength;
		//		int bottom=(int) (Xlength*0.2);
		//				int right=mCenterPrams.rightMargin;
		//		int top=(int) (Xlength*0.2);

		//����߻����ľ���
		int left=(int) Math.min(screen8, Xlength);



		if(Xlength<=screen8){
			//����Y�᷽������ű���
			mView.setScaleY(1-scale);
			//��������ƶ��ľ���   �������ƶ��ľ���  ��󲻳�����Ļ��80%
			mCenterPrams.leftMargin=left;
			//�����ұ��ƶ��ľ���  ��Ļ�Ŀ�ȼ��������ƶ��ľ���
			mCenterPrams.rightMargin=-left;
			//����X���ϵ����ű���
			//			mCenterPrams.setMargins(left, top, 0, bottom);
			mView.setLayoutParams(mCenterPrams);



		}


		//��˵���ʱ����20%������˵���ʾ���ٶ�����ָ�ƶ���1/4
		int leftSlide=Xlength/4;
		//			mLLlayout.seta
		mLeftParams=(LayoutParams) mllLeftlayout.getLayoutParams();
		//			mLeftParams.set
		//������ָ������������ʾ��˵��ľ���  ��˵�ֻ������Ļ��20%
		//		mLeftParams.leftMargin=Math.min(0,(int)-(mScreenWith*0.2)+leftSlide);

		//������߾�
		int LeftMenuLeft=(int)-(mScreenWith*0.2)+leftSlide;

		//������ʱ������˵� ���±߾�������  Ҳ�ǻ�������곤�ȵ�1/4
		//		int LeftMenuTop=Math.max(0,(int)(screen2-Xlength/4));
		//	(int) (mScreenWith*0.2)
		//�����ұ߾�  ����ܲ����Ļ��20%
		//		int LeftMenuRight=(int) Math.max((mScreenWith*0.2), (mScreenWith*0.4)-(Xlength/4));
		mLeftParams.leftMargin=Math.min(0, LeftMenuLeft); 

		//		//��˵�Y�᷽�����ű���
		mllLeftlayout.setScaleY((float) (0.8+Math.min(0.2, scale)));
		//������˵�X���ϵ����ű���
		mllLeftlayout.setScaleX((float) (0.8+Math.min(0.2, scale)));
		mllLeftlayout.setLayoutParams(mLeftParams);



	}

	/**
	 * ��ʼ�����Ҳ˵�
	 */
	private void initMenuShow() {
		// ��ȡ��Ļ���
		getScreenWidth();

		mCenterPrams= (LayoutParams) mView.getLayoutParams();
		// �������˵��Ŀ�� mScreen*0.2,leftMarin��ֵ
		// ��ȡ���˵����ֲ������
		mLeftParams = (LayoutParams) mllLeftlayout.getLayoutParams();
		//�����ұ߲˵�����   ֻ����20%
		mRightParams= (LayoutParams) mllRightLayout.getLayoutParams();
		setLeftShow(mLeftParams);
		setRightShow(mRightParams);
	}

	/**
	 * @param paramsRight
	 */
	private void setRightShow(LayoutParams paramsRight) {
		// 2)�������˵��Ŀ��
		paramsRight.width = (int) (mScreenWith * 0.8);
		// 3)�������˵���leftMargin��ֵ�����˵���ȵĸ�ֵ
		paramsRight.leftMargin =mScreenWith;
		// 4)�����������˵��Ĳ��ֲ���
		mllRightLayout.setLayoutParams(paramsRight);
	}

	/**
	 * @param params
	 */
	private void setLeftShow(LayoutParams params) {
		screen8=(int)(mScreenWith * 0.8);
		// 2)�������˵��Ŀ��
		params.width =  screen8;
		mleftScreenLength=screen8;
		// 3)�������˵���leftMargin��ֵ�����˵���ȵĸ�ֵ
		//������Ļ20%��λ��
		int magin=(int) (mScreenWith*0.2);
		screen2=magin;

		//		//��˵�X�����ű���
		//		mllLeftlayout.setScaleX(scaleX);
		//��˵�Y�᷽�����ű���
		mllLeftlayout.setScaleY((float) 0.8);
		//����X���ϵ����ű���
		mllLeftlayout.setScaleX((float) 0.8);
		params.leftMargin = (int) -(mScreenWith*0.2);
		
		//		params.setMargins((int) -(mScreenWith*0.2), magin, (int) (mScreenWith*0.4), magin);
		// 4)�����������˵��Ĳ��ֲ���
		mllLeftlayout.setLayoutParams(params);
	}

	/**
	 * //��ȡ��Ļ���
	 */
	private void getScreenWidth() {
		// ��ȡ��Ļ���
		// ��ȡWindowManagerʵ��
		WindowManager window = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);
		mScreenWith = window.getDefaultDisplay().getWidth();
	}

	/**
	 *�����м䲼�ֲ���  ���ֻ���ؽ����80%  ��ʣ20%
	 */
	public void  setCenterMenuParams(){

	}


	/**
	 * ��ʾ��߲˵�
	 */
	public void showLeftMenuListener() {
		new SlideLeftTask(true, true).execute();
	}




	/**
	 * ������߲˵�
	 */
	public void hideLeftMenuListener() {
		new SlideLeftTask(false, true).execute();
	}






	public class SlideLeftTask extends AsyncTask<Void, Void, Void> {
		boolean showMenu;
		boolean   left;//�����߲˵�  Ϊtrueʱ���ʾ��ʱ���������ʾ Ϊfalse���ʱ���ڽ����Ҳ����
		public SlideLeftTask(boolean isShow, boolean   left) {
			super();
			this.showMenu = isShow;
			this.left=left;
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			hideFinish=true;

		}
		@Override
		protected Void doInBackground(Void... params) {
			LayoutParams param = getCenterParam();

			// ����ÿ���ƶ�10px��ѭ������
			int count = param.width / 10;
			// ѭ�������˵�ÿ�������ƶ�10px
			for (int i = 1; i <= count; i++) {
				publishProgress();
				SystemClock.sleep(10);// ��ͣ10����
			}
			if(left){
				//�������߲˵�  ��ֵ����߲˵�
				D.LeftMenu.isShow=showMenu;
			}else{
				//������ʾ״̬��ֵ���Ҳ˵�
				D.RightMenu.isRightShow=!showMenu;
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			//��ȡparam ��ֵ
			LayoutParams param = getCenterParam();

			if (showMenu) {
				param.leftMargin+=10;
				//��������˵�
				if(left){
					//��ʱ�����಻�ܻ���0����ұ�
					param.leftMargin=Math.min(0, param.leftMargin);

				}else{
					//������Ҳ˵�

					//����ܲݹ���Ļ�Ŀ��
					param.leftMargin=Math.min(mScreenWith,param.leftMargin);
				}


			} else {
				param.leftMargin -= 10;// �������˵�
				if(left){//�������˵�
					//�����ƶ�����ܳ�����Ļ��-80%
					param.leftMargin=(int) Math.max(-(mScreenWith*0.8), param.leftMargin);
				}else{//������Ҳ˵�
					//������ߵľ��벻��С����Ļ��20%
					param.leftMargin=(int) Math.max((mScreenWith*0.2), param.leftMargin);


				}

			}
			// ֪ͨϵͳ����mLayoutLeftMenu��onDraw()�ػ�
			mRLlayout.invalidate();

			if(left){
				mllLeftlayout.setLayoutParams(param);
			}else{
				mllRightLayout.setLayoutParams(param);
			}

		}


	}

	/*��ListView��GridView�ȿؼ����б������¼�����Ӧ
	 * ���뱣�浽mItemClickListener������
	 */
	public void setItemClickLisener(OnItemClickListener listener){
		this.mItemClickListener=listener;
	}
	/**
	 * //��ȡparam ��ֵ
	 * @return
	 */
	private LayoutParams getCenterParam() {
		// ��ȡ���˵��Ĳ��ֲ���
		LayoutParams param = null;
		//		if(center){
		// ��ȡ���˵��Ĳ��ֲ���
		param = (LayoutParams) mView.getLayoutParams();
		//		}else{//��ȡ�ұ߲�˵��Ĳ��ֲ���
		//			param=(LayoutParams) mllRightLayout.getLayoutParams();
		//		}
		return param;
	}
}

